package nc.bs.gl.cashflowcase;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import nc.bs.framework.common.NCLocator;
import nc.itf.uap.IUAPQueryBS;
import nc.jdbc.framework.processor.ColumnListProcessor;
import nc.jdbc.framework.processor.ResultSetProcessor;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDouble;

public class CashFlowCaseSubQryBO {
	
	
	public static List<String> GetGldetailAccData(String pk_voucher,String pk_accasoa,String pcid) throws BusinessException {
		StringBuffer selectSQL=new StringBuffer();
		selectSQL.append(" 	select gl_detail.pk_accasoa from gl_detail   ");
		selectSQL.append(" 	  inner join bd_accasoa on bd_accasoa.pk_accasoa=gl_detail.pk_accasoa   ");
		selectSQL.append(" 	  inner join bd_account on bd_account.pk_account=gl_detail.pk_account and  bd_account.cashtype in (1,2,3) ");
		selectSQL.append("  where gl_detail.pk_voucher='"+pk_voucher+"' and gl_detail.pk_accasoa<>'"+pk_accasoa+"' and gl_detail.dr=0    ");
		List obj=(List) getQueryService().executeQuery(selectSQL.toString(),new ColumnListProcessor());
		return obj;
	}
	
	
	public static Map<String, UFDouble> GetMapGldetailAccData(String pk_voucher,String pk_accasoa,String pcid) throws BusinessException {
		StringBuffer selectSQL=new StringBuffer();
		selectSQL.append(" 	select gl_detail.pk_accasoa,sum(creditamount+debitamount) mny1 from gl_detail   ");
		selectSQL.append(" 	  inner join bd_accasoa on bd_accasoa.pk_accasoa=gl_detail.pk_accasoa   ");
		selectSQL.append(" 	  inner join bd_account on bd_account.pk_account=bd_accasoa.pk_account and  bd_account.cashtype in (1,2,3) ");
		selectSQL.append("  where gl_detail.pk_voucher='"+pk_voucher+"' and gl_detail.pk_accasoa<>'"+pk_accasoa+"' and gl_detail.dr=0   group by gl_detail.pk_accasoa  ");
		String query = selectSQL.toString();
		Map<String, UFDouble> map = (Map<String, UFDouble>) getQueryService()
				.executeQuery(selectSQL.toString(), new ResultSetProcessor() {
					private static final long serialVersionUID = 1328093857070449900L;

					@Override
					public Object handleResultSet(ResultSet rs)
							throws SQLException {
						Map<String, UFDouble> map = new HashMap<String, UFDouble>();
						while (rs.next()) {
							map.put(rs.getString(1), getUFDouble_NullAsZero(rs.getString(2)));
						}
						return map;
					}

				});

		return map;
	}
	
	
	
	public static IUAPQueryBS getQueryService() {
		return NCLocator.getInstance().lookup(IUAPQueryBS.class);
	}
	
	
	public static UFDouble getUFDouble_NullAsZero(Object  value) {
		  if ( value == null || value.toString().trim().equals("") ){
		    return   new UFDouble(0.0) ;
		  }else if(value  instanceof  UFDouble){
		    return  (UFDouble)value ;
		  }else if(value  instanceof  BigDecimal){
		    return  new UFDouble((BigDecimal)value) ;
		  }else{
		    return  new UFDouble(value.toString().trim());
		  }
	}
	
}
